import { Controller, Get, Query, Param, ParseIntPipe } from '@nestjs/common';
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiQuery,
  ApiParam,
} from '@nestjs/swagger';
import { ArticlesService } from './articles.service';
import {
  ArticlesListQueryDto,
  ArticlesListResponseDto,
  ArticleDetailDto,
  ApiArticlesListResponseDto,
  ApiArticleDetailResponseDto,
} from './dto/articles.dto';

@ApiTags('文章')
@Controller('articles')
export class ArticlesController {
  constructor(private readonly articlesService: ArticlesService) {}

  @Get()
  @ApiOperation({ summary: '获取文章列表' })
  @ApiResponse({
    status: 200,
    description: '获取成功',
    type: ApiArticlesListResponseDto,
  })
  @ApiQuery({ name: 'page', required: false, description: '页码', example: 1 })
  @ApiQuery({
    name: 'pageSize',
    required: false,
    description: '每页大小',
    example: 20,
  })
  @ApiQuery({ name: 'categoryId', required: false, description: '分类ID' })
  @ApiQuery({
    name: 'status',
    required: false,
    description: '文章状态',
    enum: ['DRAFT', 'PUBLISHED'],
  })
  @ApiQuery({ name: 'keyword', required: false, description: '搜索关键词' })
  @ApiQuery({
    name: 'sortBy',
    required: false,
    description: '排序方式',
    enum: ['latest', 'popular', 'viewCount', 'likeCount'],
  })
  async getArticlesList(
    @Query() query: ArticlesListQueryDto,
  ): Promise<ArticlesListResponseDto> {
    return this.articlesService.getArticlesList(query);
  }

  @Get(':id')
  @ApiOperation({ summary: '获取文章详情' })
  @ApiResponse({
    status: 200,
    description: '获取成功',
    type: ApiArticleDetailResponseDto,
  })
  @ApiResponse({ status: 400, description: '文章不存在' })
  @ApiParam({ name: 'id', description: '文章ID', example: 1 })
  async getArticleDetail(
    @Param('id', ParseIntPipe) id: number,
  ): Promise<ArticleDetailDto> {
    return this.articlesService.getArticleDetail(id);
  }

  @Get('categories/list')
  @ApiOperation({ summary: '获取文章分类列表' })
  @ApiResponse({ status: 200, description: '获取成功' })
  async getCategories() {
    return this.articlesService.getCategories();
  }
}
